//
//  ChatViewController.m
//  HelloWorld
//
//  Created by Martin Le C. on 6/8/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "HelloWorldAppDelegate.h"
#import "ChatViewController.h"
#import "Msg.h"

@implementation ChatViewController
@synthesize textChat;
@synthesize inputChat;
@synthesize yahooID;
@synthesize buddyID;
@synthesize userImage;
@synthesize buddyImage;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	textChat.font = [UIFont systemFontOfSize:12];
	self.navigationItem.title = [NSString stringWithFormat:@"To:%@", buddyID];
	[self.navigationItem.titleView sizeToFit];
	//[sendButton addTarget:self action:@selector(sendText) forControlEvents:UIControlEventTouchDown];
}
- (void)viewDidAppear:(BOOL)animated  {
	[self viewAvatar];
}
- (void)viewAvatar{
	NSString* mapURL = [[NSString stringWithFormat:@"http://img.msg.yahoo.com/avatar.php?yids=%@&format=png",yahooID] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
	NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapURL]];
	UIImage* image = [[UIImage alloc] initWithData:imageData];
	[userImage setImage:image];
	mapURL = [[NSString stringWithFormat:@"http://img.msg.yahoo.com/avatar.php?yids=%@&format=png",buddyID] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
	imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapURL]];
	image = [[UIImage alloc] initWithData:imageData];
	[buddyImage setImage:image];
	[imageData release];
	[image release];
} 
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
	// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
	if (theTextField == inputChat) {
		[inputChat resignFirstResponder];
        // Invoke the method that changes the greeting.
        [self sendMsg];
		inputChat.text = @"";
	}
	return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}
- (void) sendMsg {
	
	CGPoint p = [textChat contentOffset];
	textChat.text = [NSString stringWithFormat:@"%@\n%@: %@", textChat.text, yahooID, inputChat.text];
	[textChat setContentOffset:p animated:NO];
	[textChat scrollRangeToVisible:NSMakeRange([textChat.text length], 0)];
	
	HelloWorldAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
	[appDelegate sendMsg:buddyID msg:inputChat.text];
	
}
- (void) getChatText:(NSMutableArray*)chatList {
	NSString* chat = [[NSString alloc] init];
	chat = @"";
	Msg* chatObject = [[Msg alloc] init];
	for (int i=0; i< [chatList count]; i++) {
		chatObject = [chatList objectAtIndex:i];
		chat = [NSString stringWithFormat:@"%@\n%@: %@", chat, chatObject.f, chatObject.text];
	}
	textChat.text = chat;
	[chatObject dealloc];
}
- (void) receiveMsg:(Msg*)chatObject {
	//CGPoint p = [textChat contentOffset];
	textChat.text = [NSString stringWithFormat:@"%@\n%@: %@", textChat.text, chatObject.f, chatObject.text];
	//[textChat setContentOffset:p animated:NO];
	//[textChat scrollRangeToVisible:NSMakeRange([textChat.text length], 0)];
}
- (void)dealloc {
    [textChat release];
	[inputChat release];
	[super dealloc];
}

@end
